<?php

namespace app\admin\controller;

use app\admin\common\Base;
use think\Db;
use app\admin\model\Unit as Model;

class Unit extends Base
{

    // +----------------------------------------------------------------------
    // | 单计量单位
    // +----------------------------------------------------------------------

    public function index()
    {
        $data = $this->request->param();

        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $map = '';
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);


            if ($map == '') {
                $list = Db::name('unit')->where('is_del', 0)->where('pid', -1)->where('enterprise_id', $this->EnterpriseId)->order('id asc')->limit($start, $limit)->select();
                $count = Db::name('unit')->where('enterprise_id', $this->EnterpriseId)->where('pid', -1)->where('is_del', 0)->count();
            } else {
                $list = Db::name('unit')->where('is_del', 0)->where('pid', -1)->order('id asc')->where('enterprise_id', $this->EnterpriseId)->limit($start, $limit)->select();
                $count = Db::name('unit')->where('enterprise_id', $this->EnterpriseId)->where('pid', -1)->where('is_del', 0)->count();
            }

            return $this->grid_data($list, $count, $page, $limit);
        }
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch();
    }

    public function edit()
    {
        $data = $this->request->param();
        $info = Db::name('unit')->where('id', $data['id'])->find();
        $this->assign('info', $info);
        return $this->fetch();
    }

    public function save()
    {
        $data = $this->request->param();
        $model = new Model();
        if (isset($data['pname'])) {
            //多计量单位
            if (isset($data['id'])) {
                $arr['name'] = $data['pname'];
                $model->allowField(true)->save($arr, ['id' => $data['id']]);
                Db::name('unit')->where('pid', $data['id'])->update(['is_del' => 1]);
                $lastid = $data['id'];
            } else {
                $arr['enterprise_id'] = $this->EnterpriseId;
                $arr['name'] = $data['pname'];
                $arr['pid'] = 0;
                $model->allowField(true)->save($arr);
                $lastid = Db::name('unit')->where(['enterprise_id' => $this->EnterpriseId, 'is_del' => 0])->getLastInsID();
            }
            //更新儿子
            $son = [];
            foreach ($data['name'] as $key => $value) {
                if (!empty($value) && !empty($data['rate'][$key])) {
                    $son[$key]['name'] = $value;
                    $son[$key]['rate'] = $data['rate'][$key];
                    $son[$key]['pid'] = $lastid;
                    $son[$key]['enterprise_id'] = $this->EnterpriseId;
                }
            }
            sort($son);
            $model = new Model();
            $model->saveAll($son);
        } else {
            //单计量单位
            $data['enterprise_id'] = $this->EnterpriseId;
            if (isset($data['id'])) {
                $model->allowField(true)->save($data, ['id' => $data['id']]);
            } else {
                $model->allowField(true)->save($data);
            }
        }

        if ($model) {
            return json(['status' => 1, 'msg' => '操作成功']);
        } else {
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }

    public function del()
    {
        $data = $this->request->param();
        if (Db::name('unit')->where('id', $data['id'])->value('pid') == 0) {
            Db::name('unit')->where('pid', $data['id'])->update(['is_del' => 1]);
        }
        $res = Db::name('unit')->where('id', $data['id'])->update(['is_del' => 1]);
        if ($res) {
            return json(['status' => 1, 'msg' => '删除成功']);
        } else {
            return json(['status' => 0, 'msg' => '删除失败']);
        }
    }

    // +----------------------------------------------------------------------
    // | 多计量单位
    // +----------------------------------------------------------------------

    public function multiindex()
    {
        $data = $this->request->param();

        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $map = '';
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            if ($map == '') {
                $list = Db::name('unit')->where('pid', 0)->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->order('id asc')->limit($start, $limit)->select();
                $count = Db::name('unit')->where('pid', 0)->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            } else {
                $list = Db::name('unit')->where('pid', 0)->where('is_del', 0)->order('id asc')->where('enterprise_id', $this->EnterpriseId)->limit($start, $limit)->select();
                $count = Db::name('unit')->where('pid', 0)->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            }

            foreach ($list as $key => $value) {
                $sonUnit = Db::name('unit')->where('pid', $value['id'])->where('is_del', 0)->select();
                $list[$key]['super_name'] = $value['name'];
                foreach ($sonUnit as $k => $v) {
                    $list[$key]['super_name'] .= ',' . $v['name'];
                }
                $list[$key]['super_name'] .= '(' . $value['rate'];
                foreach ($sonUnit as $k => $v) {
                    $list[$key]['super_name'] .= ':' . $v['rate'];
                }
                $list[$key]['super_name'] .= ')';
            }

            return $this->grid_data($list, $count, $page, $limit);
        }
        return $this->fetch();
    }

    public function multiadd()
    {
        return $this->fetch();
    }

    public function multiedit()
    {
        $data = $this->request->param();
        $info = Db::name('unit')->where('id', $data['id'])->find();
        $info['son'] = Db::name('unit')->where('pid', $info['id'])->where('is_del', 0)->select();

        $this->assign('info', $info);
        return $this->fetch();
    }
}
